package com.dd.census.controller;

import javax.servlet.http.HttpServletRequest;
import org.mindrot.jbcrypt.BCrypt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.dd.census.controller.common.BaseController;
import com.dd.census.entity.SysUser;
import com.dd.census.service.SysUserService;
import com.dd.census.tools.SysException;
import com.dd.census.tools.validator.ValidateUtil;

@Controller
@RequestMapping(value = "rest/sysUser")
public class SysUserController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(SysUserController.class);

	@Autowired
	private SysUserService userService;

	@RequestMapping("select")
	public ResponseEntity<Object> select(@RequestBody SysUser user, HttpServletRequest request) {
		try {
			// 基本输入验证
			ValidateUtil.validate(user);
			return this.doSuccess(request, userService.selectPage(user));

		} catch (SysException e) {
			// 业务异常
			return doFaild(request, e);

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("/{id}")
	public ResponseEntity<Object> getById(@PathVariable("id") Integer id, HttpServletRequest request) {
		try {

			return this.doSuccess(request, userService.getSysUserById(id));
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("update")
	public ResponseEntity<Object> update(HttpServletRequest request, @RequestBody SysUser user) {

		try {
			// 基本输入验证
			ValidateUtil.validate(user);

			// 修改数据
			user.setUpdateUserId(this.getUserId(request));
			if (user.getPassword() != null && user.getPassword() != "") {
				String pass = BCrypt.hashpw(user.getPassword(), BCrypt.gensalt());
				user.setPassword(pass);
			}
			return this.doSuccess(request,userService.update(user));
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("updatePwd")
	public ResponseEntity<Object> updatePwd(HttpServletRequest request, @RequestBody SysUser user) {

		try {
			// 基本输入验证
			ValidateUtil.validate(user);
			String pass = "";
			if (user.getPassword() != null && user.getPassword() != "") {
				pass = BCrypt.hashpw(user.getPassword(), BCrypt.gensalt());
			}

			// 修改数据
			SysUser oldUser = userService.getSysUserById(user.getId());
			if(!BCrypt.checkpw(user.getPassword(), pass)){
				return  this.doSuccess(request,"-1");
			}
			user.setUpdateUserId(this.getUserId(request));
			userService.updatePassword(user);

			return this.doSuccess(request);
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("updateNew")
	public ResponseEntity<Object> updateNew(HttpServletRequest request, @RequestBody SysUser user) {

		try {
			// 基本输入验证
			ValidateUtil.validate(user);

			SysUser oldUser = userService.getSysUserById(user.getId());
			boolean p = BCrypt.checkpw(user.getPassword(), oldUser.getPassword());
			if (p) {
				// 修改数据
				user.setUpdateUserId(this.getUserId(request));
				String pass = BCrypt.hashpw(user.getNewPassword(), BCrypt.gensalt());
				user.setPassword(pass);
				userService.updatePass(user);
				return this.doSuccess(request, p);
			} else {
				throw new SysException(HttpStatus.FORBIDDEN);
			}
		} catch (SysException e) {
			return doFaild(request, e);
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	@RequestMapping("enableAndDisable")
	public ResponseEntity<Object> enableAndDisable(HttpServletRequest request, @RequestBody SysUser user) {

		try {
			// 基本输入验证
			ValidateUtil.validate(user);
			return this.doSuccess(request, userService.updatePass(user));
		} catch (SysException e) {
			return doFaild(request, e);
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("insert")
	public ResponseEntity<Object> insert(HttpServletRequest request, @RequestBody SysUser user) {

		try {
			// 基本输入验证
			ValidateUtil.validate(user);

			// 插入数据
			user.setCreateUserId(this.getUserId(request));
			// 默认密码123456
			String pass = BCrypt.hashpw("123456", BCrypt.gensalt());
			user.setPassword(pass);

			return this.doSuccess(request,userService.insert(user));
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("selectAll")
	public ResponseEntity<Object> selectAll(@RequestBody SysUser user, HttpServletRequest request) {
		try {
			// 基本输入验证
			ValidateUtil.validate(user);
			return this.doSuccess(request, userService.selectListAll(user));

		} catch (SysException e) {
			// 业务异常
			return doFaild(request, e);

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
}